package cn.iocoder.yudao.module.system.dal.mysql.erp.warehouseinventory;
import java.util.*;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.system.controller.admin.erp.warehouseinventory.vo.WarehouseInventoryPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.erp.warehouseinventory.WarehouseInventoryDO;
import cn.iocoder.yudao.module.system.dal.dataobject.erp.warehouseinventorysku.WarehouseInventorySkuDO;
import org.apache.ibatis.annotations.Mapper;

/**
 * 仓库库存 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface WarehouseInventoryMapper extends BaseMapperX<WarehouseInventoryDO> {

    default PageResult<WarehouseInventoryDO> selectPage(WarehouseInventoryPageReqVO reqVO) {
        LambdaQueryWrapperX<WarehouseInventoryDO> query = new LambdaQueryWrapperX<WarehouseInventoryDO>()
                .betweenIfPresent(WarehouseInventoryDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(WarehouseInventoryDO::getWarehouseId, reqVO.getWarehouseId())
                .eqIfPresent(WarehouseInventoryDO::getCustomerId, reqVO.getCustomerId())
                .eqIfPresent(WarehouseInventoryDO::getCheckBy, reqVO.getCheckBy())
                .betweenIfPresent(WarehouseInventoryDO::getCheckTime, reqVO.getCheckTime());
        if(StrUtil.isNotBlank(reqVO.getWarehouse())){
            query.apply("(SELECT COUNT(0)>0 FROM erp_warehouse w WHERE w.id=t.customer_id and w.deleted=0 AND w.name LIKE '%"+reqVO.getWarehouse()+"%')");
        }
        if(StrUtil.isNotBlank(reqVO.getCustomer())){
            query.apply("(SELECT COUNT(0)>0 FROM system_users u WHERE u.id=t.customer_id and u.deleted=0 AND u.nickname LIKE '%"+reqVO.getCustomer()+"%')");
        }
        if(StrUtil.isNotBlank(reqVO.getSku())){
            query.apply("(SELECT COUNT(0)>0 FROM system_users u WHERE u.id=t.customer_id and u.deleted=0 AND u.nickname LIKE '%"+reqVO.getSku()+"%')");
        }
        query.orderByDesc(WarehouseInventoryDO::getId);
        return selectPage(reqVO, query);
    }

}